package com.tuyue.service;

import com.tuyue.domain.User;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @Author: 王金海
 * @Description:
 * @Date: Created by Administrator on 2017/12/20.
 * @Modified By:
 */
@Service
public class UserServiceImpl implements UserService {
    private final AtomicLong aLong=new AtomicLong();
    private final ConcurrentMap<Long ,User> concurrentMap=new ConcurrentHashMap<>();
    @Override
    public User addOrUpdata(User user) {
        Long id = user.getId();
        if (id==null) {
            id = aLong.incrementAndGet();
            user.setId(id);
        }
        user.setCruetTime(new Date());
        this.concurrentMap.put(id,user);
        return user;
    }

    @Override
    public void del(Long id) {
        this.concurrentMap.remove(id);
    }

    @Override
    public User findById(Long id) {
        return concurrentMap.get(id);
    }

    @Override
    public List<User> list() {
        return new ArrayList<>(concurrentMap.values());
    }
}
